/*
 * WVWebChromeClient.java
 *
 * Created by WindVane.
 * Copyright (c) 2014年 阿里巴巴-无线事业部. All rights reserved.
 */

package com.hybrid.webview;

import android.annotation.TargetApi;
import android.content.Context;
import android.webkit.ConsoleMessage;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
import com.hybrid.utils.TaoLog;
import com.hybrid.utils.WVConstants;

public class WVWebChromeClient extends WebChromeClient {
    private static final String TAG = "WVWebChromeClient";
    private static final long MAX_QUOTA = 20 * 1024 * 1024;
    protected Context mContext;

    public WVWebChromeClient() {
    }

    public WVWebChromeClient(Context context) {
        this.mContext = context;
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
        ((WVWebView) view).onMessage(WVConstants.NOTIFY_PAGE_PROGRESS, newProgress);
    }

    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {

        //if (WVEventService.getInstance().onEvent(WVEventId.PAGE_onConsoleMessage, consoleMessage).isSuccess)
        //    return true;

        String data = consoleMessage.message();
        //if (data != null && data.startsWith("wvNativeCallback")) {
        //    int firstPos = data.indexOf(WVNativeCallbackUtil.SEPERATER);
        //    data = data.substring(firstPos + 1);
        //
        //    int secondPos = data.indexOf(WVNativeCallbackUtil.SEPERATER);
        //    String id = data.substring(0, secondPos);
        //    data = data.substring(secondPos + 1);
        //
        //    ValueCallback<String> callback = WVNativeCallbackUtil.getNativeCallback(id);
        //    callback.onReceiveValue(data);// 执行native回调方法
        //
        //    WVNativeCallbackUtil.clearNativeCallback(id);// 清除已调用完毕的回调
        //    return true;
        //}
        if (TaoLog.getLogStatus()) {
            TaoLog.i(
                TAG,
                "onConsoleMessage:" + consoleMessage.message() + " at " + consoleMessage.sourceId() + ": "
                    + consoleMessage.lineNumber());
        }
        return super.onConsoleMessage(consoleMessage);
    }

    @Override
    @TargetApi(5)
    public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
        super.onGeolocationPermissionsShowPrompt(origin, callback);
        callback.invoke(origin, true, false);
    }

    @Override
    @SuppressWarnings("deprecation")
    @TargetApi(5)
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long quota, long estimatedDatabaseSize, long totalQuota,
                                        WebStorage.QuotaUpdater quotaUpdater) {
        if (estimatedDatabaseSize < MAX_QUOTA) {
            quotaUpdater.updateQuota(estimatedDatabaseSize);
        } else {
            quotaUpdater.updateQuota(quota);
        }
    }

    //    @Override
    //    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
    //        if (mContext instanceof Activity) {
    //            AlertDialog.Builder dlg = new AlertDialog.Builder(mContext);
    //            dlg.setMessage(message);
    //            dlg.setTitle("提示");
    //            dlg.setCancelable(true);
    //            dlg.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
    //                public void onClick(DialogInterface dialog, int which) {
    //                    result.confirm();
    //                }
    //            });
    //            dlg.setOnCancelListener(new DialogInterface.OnCancelListener() {
    //                public void onCancel(DialogInterface dialog) {
    //                    result.cancel();
    //                }
    //            });
    //            dlg.create();
    //            dlg.show();
    //            return true;
    //        } else {
    //            return super.onJsAlert(view, url, message, result);
    //        }
    //    }

    //    @Override
    //    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
    //        if (mContext instanceof Activity) {
    //            AlertDialog.Builder dlg = new AlertDialog.Builder(mContext);
    //            dlg.setMessage(message);
    //            dlg.setTitle("提示");
    //            dlg.setCancelable(true);
    //            dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    //                public void onClick(DialogInterface dialog, int which) {
    //                    result.confirm();
    //                }
    //            });
    //            dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
    //                public void onClick(DialogInterface dialog, int which) {
    //                    result.cancel();
    //                }
    //            });
    //            dlg.setOnCancelListener(new DialogInterface.OnCancelListener() {
    //                publicvoid onCancel(DialogInterface dialog) {
    //                    result.cancel();
    //                }
    //            });
    //            dlg.create();
    //            dlg.show();
    //            return true;
    //        } else {
    //            return super.onJsConfirm(view, url, message, result);
    //        }
    //    }

    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
        if (TaoLog.getLogStatus()) {
            TaoLog.i(TAG, "onJsPrompt: " + message + "; defaultValue: " + defaultValue + "; url: " + url);
        }

        //if (WVEventService.getInstance().onEvent(WVEventId.PAGE_onJsPrompt, view, url, message, defaultValue, result).isSuccess)
        //    return true;

        if (defaultValue != null && defaultValue.equals("wv_hybrid:")) {
            //WVJsBridge.getInstance().callMethod((WVWebView) view, message);
            result.confirm("");
            return true;
        }
        return false;
    }

}
